package cn.itcast.bos.web.action.base;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.boot.model.source.internal.hbm.RelationalObjectBinder.RelationalObjectResolutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import oracle.net.aso.s;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
    private Standard standard = new Standard();
    @Autowired
    private StandardService service;
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return standard;
	}
	@Action(value="standard_save",results= {@Result(name="success", type="redirect",location="/pages/base/standard.html")})
	public String save() {
		service.save(standard);
		return SUCCESS;
	}
	private int  page;
	private int rows;
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery() {
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> pageData = service.findPageData(pageable);
		Map<String, Object> map = new HashMap<>();
		//easyUI的分页插件需要获取到这两个名字的固定值，交给打datagrid;
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
    public String findAll() {
    	List<Standard> list =service.findAll();
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }
}
